﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Itop.YHPC.Model;
using System.Collections.Generic;
using System.IO;
using NPOI.HSSF.UserModel;

namespace Itop.YHPC.search {
    public partial class DownStatistics : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            string kssj = Request.QueryString["kssj"];
            string jssj = Request.QueryString["jssj"];
            string dw = Request.QueryString["dw"];
            ExportStatistics export = new ExportStatistics();
            FillData(export.GetList(kssj, jssj, dw));
        }

        public void FillData(IList<MyStatistics> list) {
            string pathdic = "";
            if (Request.Path.Contains("yhpc")) pathdic = @"\yhpc";
            FileStream fs = new FileStream(Server.MapPath(pathdic + @"\search\Template\statistics.xls"), FileMode.Open, FileAccess.Read);
            HSSFWorkbook templateWorkbook = new HSSFWorkbook(fs, true);
            HSSFSheet sheet = templateWorkbook.GetSheetAt(0);
            HSSFRow dataRow; int num = 2;
            foreach (MyStatistics main in list) {
                dataRow = sheet.GetRow(num);
                for (int i = 0; i < main.GetType().GetProperties().Length; i++) {
                    dataRow.GetCell(i).SetCellValue(main.GetType().GetProperties()[i].GetValue(main, null).ToString());
                }
                num++;
            }
            string filename = "隐患统计结果" + DateTime.Now.ToString("yyyy-MM-dd") + ".xls";
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
            Response.Clear();
            Response.BinaryWrite(WriteToStream(templateWorkbook).GetBuffer());
            Response.End();

        }

        MemoryStream WriteToStream(HSSFWorkbook templateWorkbook) {
            MemoryStream file = new MemoryStream();
            templateWorkbook.Write(file);
            return file;
        }
    }
}
